<?php

	class FTATestimonials extends FTAObject {
		
		var $renderTestimonialsFunction;
		var $categoryIdentifier;
		var $testimonialCategory;
		var $allTestimonialCategories;
		
		function FTATestimonials(){
			parent::__construct();
		
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
							
		}

		
		function getTestimonials($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docTesti');
			
			if($this->testimonialCategory){
				$this->cmsDocument->addConstraint('categorycode',$this->testimonialCategory->documentcode,'=');
			}
				
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("<<md.rank>>");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;

		}
		
		function getTestimonialCategories(){
			if (is_null($this->allTestimonialCategories)){
			
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docTestiCat');
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->resetIterator();
			
				$this->allTestimonialCategories = $this->cmsDocument->getAllDocuments();
				
				if (!is_array($this->allTestimonialCategories))
					$this->allTestimonialCategories = array();

			}
			
			return $this->allTestimonialCategories;
		}
		
		function getTestimonialCategory($categoryIdentifier){
			$this->categoryIdentifier = $categoryIdentifier;
			$allTestimonialCategories = $this->getTestimonialCategories();
			$filteredTestimonialCategory = array_filter($allTestimonialCategories, array($this, 'filter_TestimonialCategoryByCategoryId'));
			
			if (1 == count($filteredTestimonialCategory)) {
				$this->testimonialCategory = array_pop($filteredTestimonialCategory);
				return true;
			} else {
				return false;
			}
		}
		
		function filter_TestimonialCategoryByCategoryId($o){
			if (is_null($this->categoryIdentifier)) {
				return false;
			} else if (is_numeric($this->categoryIdentifier)){
				return $this->categoryIdentifier == $o->documentcode;
			} else {
				return $this->categoryIdentifier == $o->title;
			}
		}
		
		function getRandomTestimonial() {
			//get one testimonial and render it.. 
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docTesti');
					
			$this->cmsDocument->addSort("<<md.rank>>");
			
			$this->cmsDocument->setPageSize(1);
			$this->cmsDocument->addSort('<<RAND()>>');
			$this->cmsDocument->resetIterator();
			
			// echo $this->cmsDocument->countQuery();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			$this->cmsDocument->setPageSize(null);
			
			if (!$allDocuments || 1 != count($allDocuments))
				$allDocuments = null;
			
			
			$t =  $allDocuments[0];
			return $t;
			
		}
		
		function renderRandomTestimonial() {
			$testimonial = 	$this->getRandomTestimonial();

			if (is_null($testimonial));
			
			if(is_null($this->renderTestimonialsFunction)) {
				
				if(!isset($FTATestamonialsItemRenderFunction)){
					$FTATestamonialsItemRenderFunction = '$o=fta_getSharedTestimonialsObject(); echo $o->renderTestimonialsItemFunction($item);';
				}
	
				$this->renderTestimonialsFunction  = create_function('$item', $FTATestamonialsItemRenderFunction );
			}
			call_user_func($this->renderTestimonialsFunction,$testimonial);
		}
		
		function render($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
		//	print_r($this->getTestimonials());
			$testimonials = $this->getTestimonials($pageSize, $pageNumber, $sort, $direction);
			
			
			if (0 == count($testimonials))	return;
			
			if(is_null($this->renderTestimonialsFunction)) {
				
				if(!isset($FTATestamonialsItemRenderFunction)){
					$FTATestamonialsItemRenderFunction = '$o=fta_getSharedTestimonialsObject(); echo $o->renderTestimonialsItemFunction($item);';
				}
	
				$this->renderTestimonialsFunction  = create_function('$item', $FTATestamonialsItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			fta_widgetList($testimonials,"testimonialsList","altItem","firstItem","lastItem",$this->renderTestimonialsFunction);
		}
		
		function renderTestimonialsItemFunction($item) {

?>
<div class="testimonial">

	<div class="bragHeading"><?= fta_widgetH5Tag( $item->heading )?></div>
		<?= $item->content ?>

	<div class="testimonialPerson">
    	<strong><?= $item->title ?></strong><br />
<?
	if (0 < strlen($item->authorbio)){
?>    	
    	<a href="<?= $item->authorbio ?>"><?= $item->shortdescription ?></a>
<?
	} else {
		echo $item->shortdescription;		
	}
?>	
    </div>

</div>
<?			
			
		
		}
	
	}
	
	function fta_testimonials($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
		ob_start();
		$testimonialsObject = fta_getSharedTestimonialsObject();
		$testimonialsObject->render($pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();
	}
	
	function fta_randomTestimonial() {
		ob_start();
		$testimonialsObject = fta_getSharedTestimonialsObject();
		$testimonialsObject->renderRandomTestimonial();		
		return ob_get_clean();
	}
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedTestimonialsObject(){

		global $fta_;

		global $FTATestimonialsClass;
		
		if(!is_null($FTATestimonialsClass) && "FTATestimonials" != $FTATestimonialsClass){
		} else {
			$FTATestimonialsClass = "FTATestimonials";
		}
			
		if (!array_key_exists('ftaTestimonials',$fta_)){
			$fta_->ftaTestimonials = new  $FTATestimonialsClass();
		}
		return $fta_->ftaTestimonials;
	}